<?php
require('../database/db.php');
$mysql = new mysql();
include("../../tool/UpFileTool.class.php");
error_reporting(0);

/*if ($_FILES) {
	$UpFileTool=new UpFileTool('upfile');
	$UpFileTool->multiImage($w=500, $h=500,$sumbimage='../../sumbimage', $srcimage='../../srcimage');
	$images=$UpFileTool->multiSumbImage;
	$srcimages=$UpFileTool->multiSrcImage;
}*/
// 设置主图

if ($_REQUEST[num] != '') {
	$num = $_REQUEST[num];
}else{
	$num = 0;
}
// 设置主图 end

// 设置详情图主图
if ($_REQUEST[defaultXQTuOrd] != '') {
	$defaultXQTuOrd = $_REQUEST[defaultXQTuOrd];
}else{
	$defaultXQTuOrd = 0;
}
//echo json_encode(array('1' =>$num,'123' => $defaultXQTuOrd));exit();
// 设置详情图主图 end

// $images = implode(',',$imgname);
//$der=array('im'=>$images);
if ($_REQUEST['goodsid'] != '') {
	$sql = "select count(*) from goods where goodsid=" . $_REQUEST['goodsid'];
	$goods = $mysql->getAll($sql);
	if ($goods) {
		$mode = 'update';
		$where = ' where goodsid = ' . $_REQUEST[goodsid];
	}else{
		$mode ='insert';
		$where = ' where 1 limit 1';
	}
}else{
	$mode ='insert';
	$where = ' where 1 limit 1';
}



$shopid=trim($_REQUEST[shopid]);
$name=trim($_REQUEST[name]);
$shelves=trim($_REQUEST[shelves]);
$price=trim($_REQUEST[price]);
$original=trim($_REQUEST[original]);
$can_fanquan=trim($_REQUEST[can_fanquan]);
$son_class=trim($_REQUEST[son_class]);
$guige=trim($_REQUEST[guige]);
$images=trim($_REQUEST[images]);
$srcimages=trim($_REQUEST[srcimages]);
$xiangQingTu=trim($_REQUEST[xiangQingTu]);
$srcXiangQingTu=trim($_REQUEST[srcXiangQingTu]);
$start_num=trim($_REQUEST[start_num]);
$Freight_dir=trim($_REQUEST[Freight_dir]);
$goods_dir=trim($_REQUEST[goods_dir]);
$muid=trim($_REQUEST[muid]);
$start_num_desc=trim($_REQUEST[start_num_desc]);
// $guige=explode(';',$guige);
// $length=count($res['data']);

$table='goods';

$arr=array(
	'title'=>$name,
	'shangjia'=>$shelves,
	'price'=>$price,
	'yuanjia'=>$original,
	'can_fanquan'=>$can_fanquan,
	'zixilie'=>$son_class,
	'images'=>$images,
	'srcimages'=>$srcimages,
	'defaultImgOrd'=>$num,
	'xiangQingTu'=>$xiangQingTu,
	'srcXiangQingTu'=>$srcXiangQingTu,
	'defaultXQTuOrd'=>$defaultXQTuOrd,
	'start_num'=>$start_num,
	'start_num_desc'=>$start_num_desc,
	'Freight_dir'=>$Freight_dir,
	'goods_dir' => $goods_dir,
	'muid'=>$muid,
	'shopid'=>$shopid
);

$res = $mysql->autoExecute($table,$arr,$mode,$where);
if ($mode == 'update') {
	$result = $_REQUEST[goodsid];
}else{
	$result = $mysql->insert_id();
}

$sql="DELETE from goodsselect where goodsid=" . $result;
$de = $mysql->query($sql);

/*for($i=0;$i<count($res);$i++) {
	$num = 
	for($i=0;$i<$length;$i++){
		$ar = $res[$i]['data'][$i];
		$tab ='goodsselect';
		$ary = array(
			'goodsid'=>$result,
			'fTitle'=>$res['fTitle'],
			'sTitle'=>$ar['name'],
			'price'=>$ar['price'],
			'number'=>$ar['num']
		);

		$mysql->autoExecute($tab,$ary);
	}
}*/

if($_POST['guige'] != null) {
	$res = json_decode($_POST['guige'],true);
	foreach($res as $rkey => $rvalue) {
		if(empty($res[$rkey]['data'])) {
			$tab ='goodsselect';
			$ary = array(
				'goodsid' => $result,
				'fTitle' => $rvalue['fTitle'],
				'price' => $rvalue['price'],
				'number' => $rvalue['num']
			);
			$lala = $mysql->autoExecute($tab,$ary);
		} else {
			foreach($res[$rkey]['data'] as $key => $value) {
				$tab ='goodsselect';
				$ary = array(
					'goodsid' => $result,
					'fTitle' => $rvalue['fTitle'],
					'sTitle' => $value['name'],
					'price' => $value['price'],
					'number' => $value['num']
				);
				$lala = $mysql->autoExecute($tab,$ary);
			}
		}
	}
}

$result2 = $mysql->insert_id();
if($result != false or $result2 != false){
	$der = array('code'=>100);
}else{
	$der = array('code'=>200);
}

echo json_encode($der);
?>